package com.wmzdq.photocenter.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wmzdq.photocenter.common.PropertyUtil;

@Component
public class LocalOperator implements IFileOperator {
	
	private String local="D:\\photos";
	//private final String local="/lxy/photos";
	
	
	private final String thumpath="thum";
	private final String videopath="mov";
		
	@Autowired
	PropertyUtil proUtil;
	
	public LocalOperator(){
		String os = System.getProperty("os.name");  
		if(!os.toLowerCase().startsWith("win")){  
			local="/lxy/photos";
		}else{
			local="D:\\photos";
		}
	}

	@Override
	public String[] getDirectories(String path) {
		//当前路径
//		currentPath.set(path);
//		List<String> results=new ArrayList<String>();
//		
//		
//		File currentFile;
//		if(StringUtils.isBlank(path)){
//			currentFile=new File(local);
//		}else{
//			currentFile=new File(local+File.separator+currentPath.get());
//		}
//		if(currentFile.isDirectory()){
//			File[] files=currentFile.listFiles();
//			for (File file : files) {
//				if(file.isDirectory()){
//					results.add(file.getName());
//				}
//			}
//		}
		
		return proUtil.keys();
		
		//String[] str={};
		//return results.toArray(str);
		
	}

	@Override
	public String[] getFileNames(String path) {
		String current=local+File.separator+path;
		List<String> results=new ArrayList<String>();
		
		
		File currentFile = new File(current);
//		if(StringUtils.isBlank(current)){
//			currentFile=new File(local+File.separator+path);
//		}else{
//			currentFile=new File(local+File.separator+currentPath.get()+File.separator+path);
//		}
		if(currentFile.isDirectory()){
			Collection<File > files=FileUtils.listFiles(currentFile, new String[]{"JPG","jpg","png","jpeg"},false);
			//File[] files=currentFile.listFiles();
			for (File file : files) {
				if(file.isFile()){
					results.add(file.getName());
				}
			}
		}
		
		String[] str={};
		return results.toArray(str);
		
	}

	@Override
	public void upload() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String filename,String path) {
		String filepath=getLocal()+File.separator+path+File.separator+filename;
		File file=new File(filepath);
		file.delete();
		filepath=getLocal()+File.separator+path+File.separator+getThum()+File.separator+filename;
		file=new File(filepath);
		file.delete();

	}

	@Override
	public boolean hasPre() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getLocal() {
		return local;
	}
	
	@Override
	public boolean create(String name){
		String path=getLocal()+File.separator+name;
		File file=new File(path);
		return file.mkdir();
	}
	
	@Override
	public String createThum(String filepath,String name){
		File thum=new File(filepath+File.separator+thumpath);
		if(!thum.exists()){
			thum.mkdir();
		}
		
		File file=new File(filepath+File.separator+name);
		thum=new File(filepath+File.separator+thumpath+File.separator+name);
		try {
			Thumbnails.of(file).scale(0.5f) .outputQuality(0.5f) .outputFormat("jpg").toFile(thum);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return filepath+File.separator+thumpath+File.separator+name;
	}

	@Override
	public String getThum() {
		return thumpath;
	}

	@Override
	public String getVideo(String path, String name) {
		name=name.substring(0,name.indexOf("."));
		String mov=getLocal()+File.separator+path+File.separator+videopath+File.separator+name+".MOV";
		
		File movFile=new File(mov);
		if(movFile.exists()){
			return videopath+File.separator+name+".MOV";
		}
		return "";
	}

}
